package com.pphh.demo;


import com.codahale.metrics.Counter;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.Timer;
import com.pphh.demo.metric.MetricsFactory;
import com.pphh.demo.metric.QueueManager;

import java.util.Random;

/**
 * Please add description here.
 *
 * @author huangyinhuang
 * @date 11/6/2018
 */
public class MetricsTest {

    private Meter meter;
    private Histogram histogram;
    private Counter counter;
    private Timer timer;
    private QueueManager queueManager;

    public MetricsTest() {
        this.meter = MetricsFactory.getInstance().getMeter("request-meter");
        this.histogram = MetricsFactory.getInstance().getHistogram("request-throughput");
        this.counter = MetricsFactory.getInstance().getCounter("jobs-in-pending-queue");
        this.timer = MetricsFactory.getInstance().getTimer("time-cost");
        this.queueManager = MetricsFactory.getInstance().getQueueManager();
    }

    public void run() {
        this.meter.mark();
        this.histogram.update(new Random().nextInt(10));
        this.counter.inc();
        this.queueManager.enque();

        Timer.Context context = this.timer.time();
        try {
            Thread.sleep(new Random().nextInt(1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            context.stop();
        }
    }
}
